/**
 * Created by user on 2015/12/24.
 */

angular.module('mis.scoreShopManager.controllers', [])
    .controller('scoreShopCtrl', ['$scope', '$http', function ($scope, $http) {

        $scope.editData = {};
        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //分页
        $scope.pagination = new pagination({
            currentPage: $scope.currentPage, pageSize: $scope.pageSize,
            url: 'http://' + backOfficeHost + '/rest/v1.0/mis/master/scoreShopList/:page/:size',
            ajax: $http,
            success: function (data) {
                data.data.list.forEach(function (temp) {
                    temp.images = 'http://' + imgHost + temp.images;
                });
                $scope.scoreShopList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function (err) {
                //$scope.communityList = null;
            },
            params: {}
        });


        //搜索
        $scope.search = function (current) {

            if (current) {
                $scope.currentPage = $scope.pagination.currentPage;
            } else {
                $scope.pagination.currentPage = 1;
                $scope.currentPage = 1;
            }

            var body = {};

            if ($scope.name) body.name = $scope.name;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/master/scoreShopList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                data.data.list.forEach(function (temp) {
                    temp.images = 'http://' + imgHost + temp.images;
                });
                $scope.scoreShopList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;
            }).error(function (err) {
                alert(err);
            });

        };

        $scope.selectFile = function (files, model) {
            if (model == 'add') {
                document.getElementById("image_result").innerHTML = '';
            } else {
                document.getElementById("image_result_edit").innerHTML = '';
            }
            showImages(model);
        };

        var reader = null;
        if (FileReader) {
            reader = new FileReader();
        } else {
            alert("浏览器暂时不支持FileReader");
        }
        var fileList = {}, length = 0, length1 = 0;
        var showImages = function (model) {

            if (model == 'add') {
                var imageEle = document.getElementById("images_upload");
            } else {
                var imageEle = document.getElementById("images_upload_edit");
            }

            //获取上传的多个文件
            fileList = imageEle.files;
            length = fileList.length;
            length1 = fileList.length;

            //获取文件信息
            readURL(model);
        };

        var readURL = function (model) {

            reader.readAsDataURL(fileList[length1 - length]);
            reader.onload = function () {
                var img = document.createElement("img");
                img.src = reader.result;
                img.width = 200;
                if (model == 'add') {
                    document.getElementById("image_result").appendChild(img);
                } else {
                    document.getElementById("image_result_edit").appendChild(img);
                }
                length--
                if (length == 0)return;
                readURL(model);
            };
        };

        //新增
        $scope.add = function (addData, target) {

            var fd = new FormData();
            if (fileList != null) {
                for (var i = 0; i < fileList.length; i++) {
                    var index = i + 1;
                    fd.append('image' + index, fileList[i]);
                }
                console.log(fileList[i]);
            }
            ;

            //if(addData.image)fd.append('image1', addData.image);
            if (addData.name)fd.append('productName', addData.name);
            if (addData.desc)fd.append('productDesc', addData.desc);
            if (addData.inventory)fd.append('inventoryLevel', addData.inventory);
            if (addData.price)fd.append('price', addData.price);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/master/scoreShopAddUpdate', fd, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            }).success(function (data) {
                $scope.addData = {};
                $scope.commList = [];
                $('#doc-modal-add').modal("close");
                $scope.search(true);
                alert("添加商品成功");


            }).error(function (data) {
            });


        };


        //编辑
        $scope.scoreShopEdit = function (scoreShop, target) {
            /***
             <td>{{scoreShop.id}}</td>
             <td>{{scoreShop.product_name}}</td>
             <td>{{scoreShop.product_desc}}</td>
             <td>{{scoreShop.inventory_level}}</td>
             <td>{{scoreShop.price}}</td>
             {{scoreShop.images}}
             scoreShop.create_time
             */

            //$scope.editData = scoreShop;

            $scope.editData.id = scoreShop.id;
            $scope.editData.name = scoreShop.product_name;
            $scope.editData.desc = scoreShop.product_desc;
            $scope.editData.inventory = scoreShop.inventory_level;
            $scope.editData.price = scoreShop.price;
            $scope.editData.images = scoreShop.images;
            $scope.editData.create_time = scoreShop.create_time;
        };

        //update
        $scope.scoreShopUpdate = function (editData) {
            console.log($scope.editData)

            var fd = new FormData();
            if (fileList != null) {
                for (var i = 0; i < fileList.length; i++) {
                    var index = i + 1;
                    fd.append('image' + index, fileList[i]);
                    console.log(fileList[i]);
                }
            }
            if(editData.id) fd.append('cId', editData.id);
            if (editData.name)fd.append('productName', editData.name);
            if (editData.desc)fd.append('productDesc', editData.desc);
            if (editData.inventory)fd.append('inventoryLevel', editData.inventory);
            if (editData.price)fd.append('price', editData.price);
            console.log(fd);

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/master/scoreShopAddUpdate', fd, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            }).success(function (data) {
                $scope.editData = {};
                $scope.commList = [];
                $('#doc-modal-edit').modal("close");
                $scope.search(true);
                alert("添加商品成功");
            }).error(function (data) {
            });

        }


    }])